package com.ranchuanyin.schoolcat.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.ranchuanyin.schoolcat.config.NettyConfig;
import com.ranchuanyin.schoolcat.domain.CatGroup;
import com.ranchuanyin.schoolcat.dto.MessageGroupDTO;
import com.ranchuanyin.schoolcat.dto.SendMessageDto;
import com.ranchuanyin.schoolcat.dto.WithdrawOneDTO;
import com.ranchuanyin.schoolcat.service.CatGroupService;
import com.ranchuanyin.schoolcat.service.PushService;
import com.ranchuanyin.schoolcat.util.RedisMessageUtil;
import com.ranchuanyin.schoolcat.vo.GroupMessagesVo;
import com.ranchuanyin.schoolcat.vo.ReceiveMessagesVo;
import com.ranchuanyin.schoolcat.vo.WithdrawMessagesVo;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class PushServiceImpl implements PushService {
    @Resource
    RedisMessageUtil redisMessageUtil;
    @Resource
    CatGroupService catGroupService;


    @Override
    public Boolean pushMsgToOne(SendMessageDto sendMessageDto) {
        ConcurrentHashMap<String, Channel> userChannelMap = NettyConfig.getUserChannelMap();
        Channel channel = userChannelMap.get(sendMessageDto.getToUserId());
        ReceiveMessagesVo vo = new ReceiveMessagesVo();
        vo.setMessage(sendMessageDto.getMessage());
        vo.setToUserId(sendMessageDto.getToUserId());
        vo.setFromUserId(sendMessageDto.getFromUserId());
        vo.setMessageDate(DateUtil.now());
        vo.setUsername(sendMessageDto.getUsername());
        vo.setAvatar(sendMessageDto.getAvatar());
        vo.setType(sendMessageDto.getType());
        vo.setId(sendMessageDto.getMessageId());
        if (Objects.isNull(channel)) {
            redisMessageUtil.addOfflineMessage(Long.valueOf(sendMessageDto.getToUserId()), vo);
            return false;
        }
        channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(vo)));
        return true;
    }

    @Override
    public void pushMsgToAll(String msg) {
        NettyConfig.getChannelGroup().writeAndFlush(new TextWebSocketFrame(msg));
    }

    @Override
    public void pushMsgToGroup(MessageGroupDTO dto) {
        CatGroup group = catGroupService.getById(dto.getGroupId());
        List<String> userIds = group.getUserIds();
        userIds.remove(dto.getMessageUserId());
        Channel channel = null;
        ConcurrentHashMap<String, Channel> userChannelMap = NettyConfig.getUserChannelMap();
        GroupMessagesVo vo = new GroupMessagesVo();
        vo.setId(dto.getMessageId());
        vo.setFromUserId(dto.getGroupId());
        vo.setMessageUserId(dto.getMessageUserId());
        vo.setUsername(dto.getUsername());
        vo.setMessage(dto.getMessage());
        vo.setMessageDate(DateUtil.now());
        vo.setMessageUser(dto.getMessageUser());
        vo.setAvatar(dto.getAvatar());
        vo.setType(dto.getType());
        for (String id : userIds
        ) {
            channel = userChannelMap.get(id);
            if (Objects.isNull(channel)) {
                vo.setMessageUserId(id);
                redisMessageUtil.addOfflineMessage(Long.valueOf(id), vo);
            } else
                channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(vo)));
        }
    }

    @Override
    public void WithdrawOne(WithdrawOneDTO dto) {
        ConcurrentHashMap<String, Channel> userChannelMap = NettyConfig.getUserChannelMap();
        Channel channel = userChannelMap.get(dto.getUserId());
        WithdrawMessagesVo vo = new WithdrawMessagesVo();
        vo.setMessageId(dto.getMessageId());
        vo.setType(3);
        if (Objects.isNull(channel)) {
            redisMessageUtil.addOfflineMessage(Long.valueOf(dto.getUserId()), vo);
            return;
        }
        channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(vo)));
    }

    @Override
    public void WithdrawGroup() {

    }
}

